package com.shell.governor.authorization.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.framework.utils.CommonFileUtil;
import com.shell.governor.authorization.po.PubResource;
import com.shell.governor.authorization.service.PubResourceService;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@RestController
public class PubResourceController
  extends BaseController
{
  @Autowired
  private PubResourceService resourceService;
  
  @RequestMapping
  @Logging(module="��������", methods="��������")
  public RetMsg addResource(PubResource res)
    throws Exception
  {
    if (this.resourceService.saveOrUpdateResource(res) != 1) {
      return initRetMsg("error", "��������������");
    }
    return initRetMsg("success", "��������������");
  }
  
  @RequestMapping
  @Logging(module="��������", methods="��������")
  public RetMsg updateResource(PubResource res)
    throws Exception
  {
    if (this.resourceService.saveOrUpdateResource(res) != 1) {
      return initRetMsg("error", "��������������");
    }
    return initRetMsg("success", "��������������");
  }
  
  @RequestMapping
  @Logging(module="��������", methods="����ID��������")
  public PubResource findResource(String resId)
    throws Exception
  {
    return this.resourceService.findResourceByResId(resId);
  }
  
  @RequestMapping
  @Logging(module="��������", methods="����������")
  public List<PubResource> selectResourceTree(PubResource res)
    throws Exception
  {
    return this.resourceService.selectResourceTree(res);
  }
  
  @RequestMapping
  @Logging(module="��������", methods="������������")
  public RetMsg updateResourceStateWithCascade(String resId, String inUse)
  {
    String msg = inUse.equals("U") ? "����" : inUse.equals("D") ? "����" : "����";
    if (this.resourceService.updateResourceStateWithCascade(resId, inUse) > 0) {
      return initRetMsg("success", msg + "����������");
    }
    return initRetMsg("error", msg + "����������");
  }
  
  @RequestMapping
  @Logging(module="��������", methods="����������������������")
  public RetMsg checkCode(PubResource resource)
    throws Exception
  {
    return initRetMsg("success", false, Boolean.valueOf(this.resourceService.checkCodeUnique(resource)));
  }
  
  @RequestMapping
  @Logging(module="��������", methods="��������id��������")
  public List<PubResource> selectResourceTree4Role(String roleId)
    throws Exception
  {
    return this.resourceService.selectResourceTree4Role(roleId);
  }
  
  @RequestMapping
  @Logging(module="��������", methods="��������������")
  public RetMsg authResourceToRole(String roleId, String resIds, String type)
    throws Exception
  {
    return this.resourceService.authResourceToRole(roleId, resIds, type);
  }
  
  @RequestMapping
  @Logging(module="��������", methods="��������ids������������")
  public List<PubResource> selectResourceTree4User(String roleIds, String userId, String entId, String appIds)
    throws Exception
  {
    return this.resourceService.selectResourceTree4User(roleIds, userId, entId, appIds);
  }
  
  @RequestMapping
  @Logging(module="��������", methods="������������������")
  public RetMsg authResourceToUser(String userId, String entId, String roleIds, String resIds)
    throws Exception
  {
    return this.resourceService.authResourceToUser(userId, roleIds, resIds, entId);
  }
  
  @RequestMapping
  @Logging(module="��������", methods="��������������sql����")
  public RetMsg genSql(String id, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    InputStream is = this.resourceService.genSql(id);
    CommonFileUtil.writeFileToClient("������������������.sql", is, response, request);
    return initRetMsg("success", "����������������������������");
  }
  
  @RequestMapping
  @Logging(module="��������", methods="������������")
  public List<PubResource> selectAllRes(String resName)
  {
    Example example = new Example(PubResource.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("resState", "E");
    if (StringUtil.notEmpty(resName)) {
      criteria.andLike("resName", StringUtil.getParameterLike(resName));
    }
    example.orderBy("res_name desc");
    List<PubResource> ress = this.resourceService.selectByExample(example);
    return ress;
  }
}
